#include "stdafx.h"
#include "Vector.h"
#include <math.h>

using namespace aiRobots;

Vector::Vector()
{
	this->Renew(3);
}

Vector::Vector(int length)
{
	this->Renew(length);
}
Vector::Vector(int length, vtype value)
{
	this->Renew(length, value);
}

Vector::Vector(int length, vtype* value)
{
	this->Renew(length, value);
}

void Vector::Renew(int length)
{
	if(length < 1)
	{
		length = 1;
	}
	else if(length > VSIZE)
	{
		length = VSIZE;
	}
	this->Length = length;
	for (int i=0; i < this->Length; i++)
	{
		this->Value[i] = 0;
	}
}

void Vector::Renew(int length, vtype value)
{
	if(length < 1)
	{
		length = 1;
	}
	else if(length > VSIZE)
	{
		length = VSIZE;
	}
	this->Length = length;
	for (int i=0; i < this->Length; i++)
	{
		this->Value[i] = value;
	}
}

void Vector::Renew(int length, vtype* value)
{
	if(length < 1)
	{
		length = 1;
	}
	else if(length > VSIZE)
	{
		length = VSIZE;
	}
	this->Length = length;
	for (int i=0; i < this->Length; i++)
	{
		this->Value[i] = value[i];
	}
}

void Vector::SetValue(vtype value)
{
	for (int i=0; i < this->Length; i++)
	{
		this->Value[i] = value;
	}
}

void Vector::SetValue(int index, vtype value)
{
	if(index < this->Length)
	{
		this->Value[index] = value;
	}
}

void Vector::SetValue(vtype* value)
{
	for (int i=0; i < this->Length; i++)
	{
		this->Value[i] = value[i];
	}
}

void Vector::GetValue(vtype* value)
{
	for (int i=0; i < Length; i++)
	{
		value[i] = Value[i];
	}
}

vtype Vector::GetValue(int index)
{
	if(index < this->Length)
	{
		return this->Value[index];
	}
	return 0;
}

void Vector::Normalize()
{
	vtype norm = this->Norm();
	if(norm != 0)
	{
		for (int i=0; i < this->Length; i++)
		{
			this->Value[i] /= norm;
		}
	}
}

vtype Vector::Norm()
{
	vtype norm = 0;
	for (int i=0; i < this->Length; i++)
	{
		norm += this->Value[i] * this->Value[i];
	}
	return sqrt(norm);
}

vtype Vector::AngleBetween(Vector &v)
{
	return acos(Dot(v) / (Norm() * v.Norm())) * (180.00 / 3.1415926);
}

vtype Vector::Dot(const Vector &v)
{
	vtype result = 0;
	if(Length == v.Length)
	{
		for(int i=0; i < Length; i++)
		{
			result = result + Value[i] * v.Value[i];
		}
	}
	return result;
}

Vector Vector::Cross(const Vector &v)
{
	Vector result(Length);
	if((Length == v.Length) && (Length == 3))
	{
		result.Value[0] = Value[1] * v.Value[2] - Value[2] * v.Value[1];
		result.Value[1] = Value[2] * v.Value[0] - Value[0] * v.Value[2];
		result.Value[2] = Value[0] * v.Value[1] - Value[1] * v.Value[0];
	}
	return result;
}

vtype Vector::ProjectOnto(Vector &v)
{
	return Dot(v)/v.Norm();
}

void Vector::Print()
{
	for(int i=0; i < Length; i++)
	{
		cout << (vtype)((int)(Value[i]*10000)) / 10000 << ", ";
	}
	cout << endl;
}

Vector Vector::operator=(const Vector &v)
{
	Length = v.Length;
	for(int i=0; i < Length; i++)
	{
		Value[i] = v.Value[i];
	}
	return *this;
}

Vector Vector::operator+(const Vector &v)
{
	Vector result(Length, &Value[0]);
	if(Length == v.Length)
	{
		for(int i=0; i < Length; i++)
		{
			result.Value[i] = Value[i] + v.Value[i];
		}
	}
	return result;
}

Vector Vector::operator-(const Vector &v)
{
	Vector result(Length, &Value[0]);
	if(Length == v.Length)
	{
		for(int i=0; i < Length; i++)
		{
			result.Value[i] = Value[i] - v.Value[i];
		}
	}
	return result;
}

Vector Vector::operator*(vtype a)
{
	Vector result(Length, &Value[0]);
	for (int i=0; i < this->Length; i++)
	{
		result.Value[i] = Value[i] * a;
	}
	return result;
}

Vector Vector::operator/(vtype a)
{
	Vector result(Length, &Value[0]);
	if(a != 0)
	{
		for (int i=0; i < this->Length; i++)
		{
			result.Value[i] = Value[i] / a;
		}
	}
	return result;
}

// Vector Vector::operator=(const Point &p)
// {
// 	Length = 3;
// 	Value[0] = p.X
// 	Value[1] = p.Y;
// 	Value[2] = p.Z;
// 	return *this;
// }